package com.kizitonwose.calendarview.utils;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Rect;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

public class Extensions {


    public static final int NO_INDEX = -1;

    public static final Component inflate(ComponentContainer componentContainer, int layoutRes, boolean attachToRoot) {
        Component component = LayoutScatter.getInstance(componentContainer.getContext()).parse(layoutRes, componentContainer, attachToRoot);
        return component;
    }


    public static final boolean orFalse(Boolean $this$orFalse) {
        return $this$orFalse != null ? $this$orFalse : false;
    }

    public static final int orZero(Integer $this$orZero) {
        return $this$orZero != null ? $this$orZero : 0;
    }

    public static final YearMonth getYearMonth(LocalDate date) {
        YearMonth yearMonth = YearMonth.of(date.getYear(), date.getMonth());
        return yearMonth;
    }

    public static final YearMonth getNext(LocalDate date) {
        YearMonth mYearMonth = getYearMonth(date).plusMonths(1L);
        return mYearMonth;
    }

    public static final YearMonth getNext(YearMonth yearMonth) {
        YearMonth mYearMonth = yearMonth.plusMonths(1L);
        return mYearMonth;
    }

    public static final YearMonth getPrevious(LocalDate date) {
        YearMonth yearMonth = getYearMonth(date).minusMonths(1L);
        return yearMonth;
    }

    public static final String getNamedString(Rect $this$namedString) {
        return "[L: " + $this$namedString.left + ", T: " + $this$namedString.top + "][R: " + $this$namedString.right + ", B: " + $this$namedString.bottom + ']';
    }

    /**
     * 分割List
     *
     * @param list     待分割的list
     * @param pageSize 每段list的大小
     * @return List<< List < T>>
     * @author bianrx
     */
    public static <T> List<List<T>> splitList(List<T> list, int pageSize) {
        List<List<T>> listArray = new ArrayList<List<T>>();
        List<T> subList = null;
        for (int i = 0; i < list.size(); i++) {
            if (i % pageSize == 0) {//每次到达页大小的边界就重新申请一个subList
                subList = new ArrayList<T>();
                listArray.add(subList);
            }
            subList.add(list.get(i));
        }
        return listArray;
    }

    public static <T> T getLast(List<T> list) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        int size = list.size();
        return list.get(size - 1);
    }

    public static <T> List<T> take(List<T> list, int num) {
        List<T> mList = null;
        int size = list.size();
        if (size <= num) {
            return list;
        }
        mList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            mList.add(list.get(i));
        }

        return mList;
    }


//
//    public static final Job getJob(CoroutineScope $this$job) {
//        Element var1 = $this$job.getCoroutineContext().get((Key)Job.Key);
//        boolean var2 = false;
//        boolean var3 = false;
//        var3 = false;
//        boolean var4 = false;
//        if (var1 == null) {
//            boolean var5 = false;
//            String var6 = "Required value was null.";
//            throw (Throwable)(new IllegalArgumentException(var6.toString()));
//        } else {
//            return (Job)var1;
//        }
//    }
//
}
